package com.taotao.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.common.EasyUIResult;
import com.taotao.common.ExcelUtils;
import com.taotao.common.ExportParaVo;
import com.taotao.common.TaotaoConstant;
import com.taotao.common.TaotaoResult;
import com.taotao.pojo.UserInfo;
import com.taotao.service.UserInfoService;

@Controller
@RequestMapping("/user/")
public class UserInfoController {
	
	@Resource
	private UserInfoService userInfoService;
	
	/**
	 * 跳转到列表页面
	 * @return
	 */
	@RequestMapping(value="toUserListPage.do",method=RequestMethod.GET)
	public String toUserListPage(){
		return "users";
	}
	
	/**
	 * 查询列表数据
	 * @param pageStart
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="selectList.do",method=RequestMethod.GET)
	@ResponseBody
	public EasyUIResult<UserInfo> selectList(@RequestParam(value="page",defaultValue="1") Integer page,
			@RequestParam(value="rows",defaultValue="5") Integer rows){
		EasyUIResult<UserInfo> userList = userInfoService.selectList(page,rows);
		return userList;
	}
	
	/**
	 * 跳转到会员信息页面（新增和修改共用一个页面）
	 * @param flag : add:新增  update:修改
	 * @param id : 	用户id
	 * @return
	 */
	@RequestMapping(value="toUserInfoPage.do",method=RequestMethod.GET)
	public String toUserInfoPage(String flag,String id,Model model){
		if(TaotaoConstant.ADD_UPDATE_FLAG.ADD_FLAG.equals(flag)){//add
			
		}else if(TaotaoConstant.ADD_UPDATE_FLAG.UPDATE_FLAG.equals(flag)){//update
			UserInfo userInfo = userInfoService.selectUserInfoById(id);
			userInfo.setBirthdayStr(new SimpleDateFormat("yyyy-MM-dd").format(userInfo.getBirthday()));
			model.addAttribute("userInfo", userInfo);
		}
		return "userInfo";
	}
	
	/**
	 * 新增
	 * @param str
	 */
	/*@RequestMapping(value="add.do",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult add(String str){
		TaotaoResult taotaoResult = new TaotaoResult();
		ObjectMapper mapper = new ObjectMapper();
		try {
			UserInfo userInfo = mapper.readValue(str, UserInfo.class);
			userInfoService.add(userInfo);
			taotaoResult.setCode(TaotaoConstant.RESULT_CODE.success);
			taotaoResult.setMsg("保存成功");
		} catch (IOException e) {
			taotaoResult.setCode(TaotaoConstant.RESULT_CODE.fail);
			taotaoResult.setMsg("保存失败");
			e.printStackTrace();
		}
		return taotaoResult;
	}*/
	
	@RequestMapping(value="add.do",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult add(UserInfo userInfo){
		TaotaoResult taotaoResult = new TaotaoResult();
		try {
			userInfoService.add(userInfo);
			taotaoResult.setCode(TaotaoConstant.RESULT_CODE.success);
			taotaoResult.setMsg("保存成功");
		} catch (Exception e) {
			taotaoResult.setCode(TaotaoConstant.RESULT_CODE.fail);
			taotaoResult.setMsg("保存失败");
			e.printStackTrace();
		}
		return taotaoResult;
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="delete.do",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult delete(String ids){
		TaotaoResult taotaoResult = new TaotaoResult();
		try {
			userInfoService.delete(ids);
			taotaoResult.setCode(TaotaoConstant.RESULT_CODE.success);
		} catch (Exception e) {
			taotaoResult.setCode(TaotaoConstant.RESULT_CODE.fail);
			e.printStackTrace();
		}
		return taotaoResult;
	}
	
	/**
	 * 导出当前页的用户(需制作模板的)
	 * @param page
	 * @param rows
	 */
	@RequestMapping(value="export.do",method=RequestMethod.POST)
	public void export(@RequestParam(value="page",defaultValue="1") Integer page,
			@RequestParam(value="rows",defaultValue="5") Integer rows,HttpServletRequest request,HttpServletResponse response){
		//获取要导出的数据
		EasyUIResult<UserInfo> result = userInfoService.selectList(page,rows);
		List<UserInfo> userList = result.getRows();
		//格式化数据
		for(UserInfo userInfo : userList){
			//性别
			userInfo.setSexStr(userInfo.getSex()==1?"男":"女");
			userInfo.setBirthdayStr(new SimpleDateFormat("yyyy-MM-dd").format(userInfo.getBirthday()));
			userInfo.setCreateDateStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(userInfo.getCreateDateTime()));
		}
		ExportParaVo paravo = new ExportParaVo();
		paravo.setData(userList);
		paravo.setFileKey("userInfo");
		paravo.setDefaultFileName(new SimpleDateFormat("yyyyMMdd").format(new Date())+"-用户信息");
		ExcelUtils.exportExcel(paravo, request, response);
	}
	
	/**
	 * 修改会员信息
	 * @param str
	 * @return
	 */
	@RequestMapping(value="update.do",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult update(String str){
		TaotaoResult taotaoResult = new TaotaoResult();
		ObjectMapper mapper = new ObjectMapper();
		try {
			UserInfo userInfo = mapper.readValue(str, UserInfo.class);
			userInfoService.update(userInfo);
			taotaoResult.setCode(TaotaoConstant.RESULT_CODE.success);
			taotaoResult.setMsg("修改成功");
		} catch (IOException e) {
			taotaoResult.setCode(TaotaoConstant.RESULT_CODE.fail);
			taotaoResult.setMsg("修改失败");
			e.printStackTrace();
		}
		return taotaoResult;
	}
	
	/**
	 * 会员信息详情
	 * @param id
	 * @return
	 */
	@RequestMapping(value="toDetailView.do",method=RequestMethod.GET)
	public String toDetailView(String id,Model model){
		UserInfo userInfo = userInfoService.selectUserInfoById(id);
		userInfo.setBirthdayStr(new SimpleDateFormat("yyyy-MM-dd").format(userInfo.getBirthday()));
		model.addAttribute("userInfo", userInfo);
		return "userDetail";
	}
	
	
}
